'''
Created on 2010/07/06

@author: yukiyonezawa
'''
from pyrfid.asn1.Decoder import Decoder

from pyrfid.DataProtocol.CommandResponseUnit.WriteObjectsSegmentedMemoryTagCRU import WriteObjectsSegmentedMemoryTagCRU
from pyrfid.DataProtocol.CommandResponseUnit.ReadObjectsCRU import ReadObjectsCRU
from pyrfid.DataProtocol.CommandResponseUnit.EraseMmeoryCRU import EraseMemoryCRU
from pyrfid.DataProtocol.CommandResponseUnit.ConfigureAFICRU import ConfigureAFICRU


class CommandResponseUnit(object):
    '''
    This class decide how to use the interrogator by Application Command 
    '''
        
    ############################################################################################
    # a  identifier of application command
    CONFIGURE_AFI_CUR = "1 0 15961 126 1"
    CONFIGURE_STORAGE_FORMAT_CRU = "1 0 15961 126 2"
    INVENTORY_TAGS_CRU = "1 0 15961 126 3"
    WRITE_OBJECTS_CRU = "1 0 15961 126 4"
    DELETE_OBJECT_CRU = "1 0 15961 126 5"
    MODIFY_OBJECT_CRU = "1 0 15961 126 6"
    READ_SINGLE_OBJECT_CRU = "1 0 15961 126 7"
    READ_OBJECT_IDENTIFIERS_CRU = "1 0 15961 126 8"
    READ_OBJECTS_CRU = "1 0 15961 126 9"
    READ_LOGICAL_MEMORY_MAP_CRU = "1 0 15961 126 10"
    IVENTORY_AND_READ_OBJECTS_CRU = "1 0 15961 126 11"
    ERASE_MEMORY_CRU = "1 0 15961 126 12"
    GET_APP_BASED_SYSTEM_INFORMATION_CRU = "1 0 15961 126 13"
    ADD_MULTIPLE_OBJECTS_CRU = "1 0 15961 126 14"
    READ_MULTIPLE_OBJECTS_CRU = "1 0 15961 126 15"
    READ_FIRST_OBJECT_CRU = "1 0 15961 126 16"
    READ_OBJECTS_CRU = "1 0 15961 126 17"
    WRITE_OBJECTS_SEGMENTED_MEMORY_TAG_CRU = "1 0 15961 126 18"
    ############################################################################################
    
    RESPONSE_CONFIGURE_AFI_CUR = "1 0 15961 127 1"
    RESPONSE_CONFIGURE_STORAGE_FORMAT_CRU = "1 0 15961 127 2"
    RESPONSE_INVENTORY_TAGS_CRU = "1 0 15961 127 3"
    RESPONSE_WRITE_OBJECTS_CRU = "1 0 15961 127 4"
    RESPONSE_DELETE_OBJECT_CRU = "1 0 15961 127 5"
    RESPONSE_MODIFY_OBJECT_CRU = "1 0 15961 127 6"
    RESPONSE_READ_SINGLE_OBJECT_CRU = "1 0 15961 127 7"
    RESPONSE_READ_OBJECT_IDENTIFIERS_CRU = "1 0 15961 127 8"
    RESPONSE_READ_OBJECTS_CRU = "1 0 15961 127 9"
    RESPONSE_READ_LOGICAL_MEMORY_MAP_CRU = "1 0 15961 127 10"
    RESPONSE_IVENTORY_AND_READ_OBJECTS_CRU = "1 0 15961 127 11"
    RESPONSE_ERASE_MEMORY_CRU = "1 0 15961 127 12"
    RESPONSE_GET_APP_BASED_SYSTEM_INFORMATION_CRU = "1 0 15961 127 13"
    RESPONSE_ADD_MULTIPLE_OBJECTS_CRU = "1 0 15961 127 14"
    RESPONSE_READ_MULTIPLE_OBJECTS_CRU = "1 0 15961 127 15"
    RESPONSE_READ_FIRST_OBJECT_CRU = "1 0 15961 127 16"
    RESPONSE_READ_OBJECTS_CRU = "1 0 15961 127 17"
    RESPONSE_WRITE_OBJECTS_SEGMENTED_MEMORY_TAG_CRU = "1 0 15961 127 18"    
    
    
    APPLICATION_COMMAND = str()
    driver = object()
    DSFID_NULL = "00"
    
    
    def __init__(self, application_command, driver):
        '''
        application_command -- a string by ApplicationInterface.
        driver -- a DriverObject
        '''
        self.APPLICATION_COMMAND = application_command
        self.driver = driver
        
    def execute(self):
        
        #decode application command
        command_list = self.__decodeApplicationCommand()
                
        cru_id = command_list.popElement().VALUE
        print "CRU", cru_id
        
        #30
        command_list.popElement().VALUE
        tag_id = command_list.popElement().VALUE
        
        #############################################################################
        #driver
        dsfid_on_tag = ""
        dsfid_on_hex = self.driver.getDSFID(tag_id)
        
        print "dsfid_on_hex is ",dsfid_on_hex
        if dsfid_on_hex == self.DSFID_NULL:
            dsfid_on_tag = ""
        else:
            dsfid_on_tag = DSFID.analyzeDSFID(dsfid_on_hex)
            #set driver to Access Method
            dsfid_on_tag.access_method.setDriver(self.driver)
        #############################################################################            
        
        #create instance of  ****CUR
        cru = object()
        response_cru_id = ""
        if cru_id == CommandResponseUnit.WRITE_OBJECTS_SEGMENTED_MEMORY_TAG_CRU:
            response_cru_id = CommandResponseUnit.RESPONSE_WRITE_OBJECTS_SEGMENTED_MEMORY_TAG_CRU
            cru = WriteObjectsSegmentedMemoryTagCRU(tag_id, dsfid_on_tag, command_list, self.driver)            
        elif cru_id == CommandResponseUnit.READ_OBJECTS_CRU:
            response_cru_id = CommandResponseUnit.RESPONSE_READ_OBJECTS_CRU
            cru = ReadObjectsCRU(tag_id, dsfid_on_tag, command_list, self.driver)        
        elif cru_id == CommandResponseUnit.ERASE_MEMORY_CRU:
            response_cru_id = CommandResponseUnit.RESPONSE_ERASE_MEMORY_CRU
            cru = EraseMemoryCRU(tag_id, self.driver)
        elif cru_id == CommandResponseUnit.CONFIGURE_AFI_CUR:
            response_cru_id = CommandResponseUnit.RESPONSE_CONFIGURE_AFI_CUR
            cru = ConfigureAFICRU(tag_id, command_list, self.driver)
        
        print cru
        cru.execute()
        #encode application command
        return cru.encode(response_cru_id)
    
    def __decodeApplicationCommand(self):
        decoder = Decoder(self.APPLICATION_COMMAND)
        return decoder
    
    def __encodeApplicationCommand(self):
        pass
    
from pyrfid.DataProtocol.DSFID.DSFID import DSFID    